package me.xiaosiqi.service.system.impl;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.xiaosiqi.dao.system.RoleDao;
import me.xiaosiqi.domain.system.Role;
import me.xiaosiqi.factory.MapperFactory;
import me.xiaosiqi.service.system.RoleService;
import me.xiaosiqi.service.system.RoleService;
import me.xiaosiqi.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.*;

/**
 * @author xiaosiqi
 * @date 2021/6/3 14:38
 */
public class RoleServiceImpl implements RoleService {

    @Override
    public void save(Role role) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            String s = UUID.randomUUID().toString();
            role.setId(s);
            roleDao.save(role);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void delete(Role role) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            roleDao.delete(role);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void update(Role role) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            roleDao.update(role);
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public Role findById(String id) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            return roleDao.findById(id);
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<Role> findAll() {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            return roleDao.findAll();
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }


    @Override
    public PageInfo findPage(int page, int size) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            PageHelper.startPage(page,size);
            List<Role> all = roleDao.findAll();
            return new PageInfo(all);
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<Role> findAllRoleByUserId(String userId) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            return roleDao.findAllRoleByUserId(userId);
        } catch (Exception e) {
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void updateRole(String userId, String[] roleIds) {
        SqlSession sqlSession=null;
        try {
            //获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Dao
            RoleDao roleDao = MapperFactory.getMapper(sqlSession, RoleDao.class);
            roleDao.deleteUserId(userId);
            if (roleIds!=null){
                roleDao.updateRole(userId,roleIds);
            }
            //提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //如果提交事务失败则回滚事务
            TransactionUtil.rollback(sqlSession);
            //并抛出运行时异常
            throw new RuntimeException(e);
            // TODO 后期添加实现记录日志功能
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

}
